#include "cpu/exec/template-start.h"

#define instr mov

static void do_execute() {
	Log("op_src->type = %d, op_dest->type = %d", op_src->type, op_dest->type);
	//如果源操作数是寄存器，更改op_src->val为正确的值
	if(op_src->type == OP_TYPE_REG ) {
		switch (op_src->reg) {
			case 0: op_src->val = cpu.eax; break;
			case 1: op_src->val = cpu.ecx; break;
			case 2: op_src->val = cpu.edx; break;
			case 3: op_src->val = cpu.ebx; break;
			case 4: op_src->val = cpu.esp; break;
			case 5: op_src->val = cpu.ebp; break;
			case 6: op_src->val = cpu.esi; break;
			case 7: op_src->val = cpu.edi; break;
			default: assert(0);
		}
	}
	//如果目的操作数是寄存器，更改op_dest->val为正确的值
	if(op_dest->type == OP_TYPE_REG) {
		switch (op_dest->reg) {
			case 0: op_dest->val = cpu.eax; break;
			case 1: op_dest->val = cpu.ecx; break;
			case 2: op_dest->val = cpu.edx; break;
			case 3: op_dest->val = cpu.ebx; break;
			case 4: op_dest->val = cpu.esp; break;
			case 5: op_dest->val = cpu.ebp; break;
			case 6: op_dest->val = cpu.esi; break;
			case 7: op_dest->val = cpu.edi; break;
			default: assert(0);
		}
	}

	// OPERAND_W(op_dest, op_src->val);
	//手动实现OPERAND_W
	if(op_dest->type == OP_TYPE_REG) {
		switch (op_dest->reg) {
			case 0: cpu.eax = op_src->val; break;
			case 1: cpu.ecx = op_src->val; break;
			case 2: cpu.edx = op_src->val; break;
			case 3: cpu.ebx = op_src->val; break;
			case 4: cpu.esp = op_src->val; break;
			case 5: cpu.ebp = op_src->val; break;
			case 6: cpu.esi = op_src->val; break;
			case 7: cpu.edi = op_src->val; break;
			default: assert(0);
		}
	}
	else if(op_dest->type == OP_TYPE_MEM) {
		Log("op_dest->addr = %x, op_src->val = %x，DATA_BYTE = %d", op_dest->addr, op_src->val, DATA_BYTE);
		hwaddr_write(op_dest->addr, DATA_BYTE, op_src->val);
	}
	else {
		assert(0);
	}
	print_asm_template2();
}

make_instr_helper(i2r)
make_instr_helper(i2rm)
make_instr_helper(r2rm)
make_instr_helper(rm2r)

make_helper(concat(mov_a2moffs_, SUFFIX)) {
	swaddr_t addr = instr_fetch(eip + 1, 4);
	// MEM_W(addr, REG(R_EAX));
	//手动实现MEM_W
	swaddr_write(addr, DATA_BYTE, cpu.eax);

	// Log("addr = %x, REG(R_EAX) = %x", addr, REG(R_EAX));
	print_asm("mov" str(SUFFIX) " %%%s,0x%x", REG_NAME(R_EAX), addr);
	return 5;
}

make_helper(concat(mov_moffs2a_, SUFFIX)) {
	swaddr_t addr = instr_fetch(eip + 1, 4);
	// REG(R_EAX) = MEM_R(addr);
	//手动实现MEM_R
	cpu.eax = swaddr_read(addr, DATA_BYTE);

	print_asm("mov" str(SUFFIX) " 0x%x,%%%s", addr, REG_NAME(R_EAX));
	return 5;
}

#include "cpu/exec/template-end.h"
